﻿using System;
using System.Collections.Generic;
using System.Text;
using SistemaLib.sipe.db.sqlserver;
using SistemaLib.Entidades;
using SistemaLib.sipe.db.general;
using SistemaLib.sipe.util;
using System.Data;

namespace SistemaLib.Persistencia
{
    public class DbTareaTiempo : DbBase
    {
        private void Agregar(TareaTiempo obj)
        {
            DbScriptGenerator sql = new DbScriptGenerator();

            sql.Tabla = "TareasTiempos";
            sql.addCampo("idTarea");
            sql.addCampo("idRH");
            sql.addCampo("fechaLog");
            sql.addCampo("horas");

            DbParameterCollection col = new DbParameterCollection();
            col.Add(DbConnection.SqlInputParam("@idTarea", DbTipo.Entero, 0, obj.IdTarea));
            col.Add(DbConnection.SqlInputParam("@idRH", DbTipo.Entero, 0, obj.IdRH));
            col.Add(DbConnection.SqlInputParam("@fechaLog", DbTipo.Fecha, 0, obj.FechaLog));
            col.Add(DbConnection.SqlInputParam("@horas", DbTipo.Float, 0, obj.Horas));


            object id;
            this.DbConnection.RunScalarProcedure(sql.getInsert(), col, CommandType.Text, out id);

            obj.Id = int.Parse(id.ToString());
        }

        private void Modificar(TareaTiempo obj)
        {
            DbScriptGenerator sql = new DbScriptGenerator();

            sql.Tabla = "TareasTiempos";
            sql.CampoClave = "id";
            sql.addCampo("idTarea");
            sql.addCampo("idRH");
            sql.addCampo("fechaLog");
            sql.addCampo("horas");

            DbParameterCollection col = new DbParameterCollection();
            col.Add(DbConnection.SqlInputParam("@id", DbTipo.Entero, 0, obj.Id));
            col.Add(DbConnection.SqlInputParam("@idTarea", DbTipo.Entero, 0, obj.IdTarea));
            col.Add(DbConnection.SqlInputParam("@idRH", DbTipo.Entero, 0, obj.IdRH));
            col.Add(DbConnection.SqlInputParam("@fechaLog", DbTipo.Fecha, 0, obj.FechaLog));
            col.Add(DbConnection.SqlInputParam("@horas", DbTipo.Float, 0, obj.Horas));

            object id;
            this.DbConnection.RunScalarProcedure(sql.getUpdate(), col, CommandType.Text, out id);
        }

        internal void Eliminar(int idTT)
        {
            string sql = "delete from TareasTiempos where id = @id";

            DbParameterCollection col = new DbParameterCollection();
            col.Add(DbConnection.SqlInputParam("@id", DbTipo.Entero, 0, idTT));

            int cant;
            this.DbConnection.RunNonQueryProcedure(sql, col, CommandType.Text, out cant);
        }

        internal void Guardar(TareaTiempo obj)
        {
            if (obj.Id == 0)
                Agregar(obj);
            else
                Modificar(obj);
        }

        private void Cargar(DataRow fila, TareaTiempo obj)
        {
            obj.Id = ObtieneCampo.TipoInt(fila, "id");
            obj.IdTarea = ObtieneCampo.TipoInt(fila, "idTarea");
            obj.IdRH = ObtieneCampo.TipoInt(fila, "idRH");
            obj.FechaLog = ObtieneCampo.TipoFecha(fila, "fechaLog");
            obj.Horas = ObtieneCampo.TipoMoneda(fila, "horas");
        }

        public TareaTiempo CargarTareaTiempo(int id)
        {
            TareaTiempo obj = new TareaTiempo();

            string sql = @"select * from TareasTiempos where id = @id";

            DataSet ds;

            DbParameterCollection col = new DbParameterCollection();
            col.Add(DbConnection.SqlInputParam("@idTarea", DbTipo.Entero, 0, id));

            this.DbConnection.RunDataSetProcedure(sql, col, CommandType.Text, out ds);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataRow fila = ds.Tables[0].Rows[0];
                Cargar(fila, obj);
            }

            return obj;
        }

        public decimal HorasConsumidas(int idTarea, int idRH)
        {
            string sql = @"select sum(horas) 
                            from TareasTiempos
                            where idRH = @idRH
                            and idTarea = @idTarea";

            DbParameterCollection col = new DbParameterCollection();
            col.Add(DbConnection.SqlInputParam("@idTarea", DbTipo.Entero, 0, idTarea));
            col.Add(DbConnection.SqlInputParam("@idRH", DbTipo.Entero, 0, idRH));

            object res;

            this.DbConnection.RunScalarProcedure(sql, col, CommandType.Text, out res);

            return res != DBNull.Value ? Convert.ToDecimal(res) : 0;
        }
    }
}
